package com.yf.service.course.impl;

import com.yf.dao.UserDao;
import com.yf.dao.course.LessonDao;
import com.yf.entity.course.LessonEntity;
import com.yf.entity.course.LessonOrderItemEntity;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.vo.CourseBackVo;
import com.yf.vo.LearningNumber;
import com.yf.vo.OptCourseTypeBackVo;
import com.yf.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yf.dao.course.CourseDao;
import com.yf.entity.course.CourseEntity;
import com.yf.service.course.CourseService;

/**
 * 课程Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Oct 23 15:40:17 CST 2018
 */
@Service
public class CourseServiceImpl extends BaseServiceImpl<CourseEntity> implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Autowired
    private LessonDao lessonDao;

    @Autowired
    private UserDao userDao;

    /**内部方法**/


    /**外部方法实现**/
    //获取所有课程
    public List<CourseEntity> getAll(){
        //先获取列表
        List<CourseEntity> list = courseDao.getAll();
        //查询课程的购买人数
        getCourseLearningNumbers(list);
        return list;
    }

    //根据类型获取课程
    @Override
    public List<CourseEntity> getCourseByType(Long typeId) {
        List<CourseEntity> list = courseDao.getCourseByType(typeId);
        //查询课程的购买人数
        getCourseLearningNumbers(list);
        return list;
    }

    @Override
    public List<CourseBackVo> courseList(Map<String, Object> params) {
        return courseDao.courseList(params);
    }


    //查询课程列表中每个课程的购买人数
    private void getCourseLearningNumbers(List<CourseEntity> list){
        if(!list.isEmpty()){
            List courseIds = new ArrayList();
            for (CourseEntity courseEntity : list) {
                courseIds.add(courseEntity.getId());
            }
            List<LearningNumber> learningNumbers = courseDao.getLearningNumber(courseIds);
            if(!learningNumbers.isEmpty()){
                for (CourseEntity courseEntity : list) {
                    for (LearningNumber learningNumber : learningNumbers) {
                        if(courseEntity.getId().equals(learningNumber.getId())){
                            courseEntity.setLearningNumber(learningNumber.getCount());
                            break;
                        }else {
                            courseEntity.setLearningNumber(0);
                        }
                    }
                }

            }

        }

    }

    //课程是否购买(未完全购买视为未购买)
    @Override
    public boolean coursePaid(Long userId, Long courseId) {
        List<LessonEntity> lessonList = lessonDao.getList(courseId);
        List<Long> list = new ArrayList<>();
        for (LessonEntity lessonEntity : lessonList) {
            list.add(lessonEntity.getId());
        }
        List<LessonOrderItemEntity> itemList = lessonDao.lessonPaid(userId, list);
        if (itemList.size() != lessonList.size()) {
            return false;
        }

        return true;
    }

    @Override
    public List<OptCourseTypeBackVo> courseTypeList() {
        return courseDao.courseTypeList();
    }

    //展示共同学习的5个人
    @Override
    public List<UserVO> getPaidUsers(Long lessonId) {
        List<Long> uIds = courseDao.getLastPaidUser(lessonId);
        List<UserVO> users = new ArrayList<>();
        if(!uIds.isEmpty()){
            users = userDao.getUserList(uIds);
        }
        return users;
    }

}
